<?php

namespace app\admin\controller;

// 官网

use app\common\model\Dynamic;
use app\common\model\News;
use app\common\model\Video;
use think\Controller;
use think\Request;

class Website extends Controller
{

    /**
     * @route('website/dynamiclist')
     * @return mixed
     * 公司动态列表
     */
    public function dynamiclist()
    {

        $dynamic = new Dynamic();

        // 获取搜索条件
        $get['status'] = input('get.status');

        // 后台账号数据
        $rs_data = $dynamic->get_list_page($get, 'status asc,sort desc', '', 10);

        $tvar = array(
            "title" => "商铺列表",
            "time" => time(),
            "data" => $rs_data,
            "get" => $get,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('website/dynamic/dynamiclist');
    }

    /**
     * @route('website/boxdynamic')
     * @return mixed
     * 编辑公司动态
     */
    public function boxdynamic(Request $request)
    {
        $dynamic = new Dynamic();

        $id = input('id');

        if ($request->isAjax()) {
            $param = $request->param();


            if (array_key_exists("imageurl", $param)) {
                $data['top_pic'] = "/upload/" . $param['imageurl'][0];
            }
            $data['title'] = $param['title'];
            $data['content'] = $param['content'];
            $data['status'] = $param['status'];
            $data['sort'] = $param['sort'];

            $rs = $dynamic->edit($data, $id);
            if ($rs === false) {
                $this->error($dynamic->getError());
            }
            $this->success('操作成功', '/website/dynamiclist');
        }

        $rs_data = null;
        if ($id > 0) {
            $rs_data = $dynamic->getOne(['id' => $id], 'title,content,status,sort');
        }

        $tvar = array(
            "title" => "添加公司动态",
            "time" => time(),
            "data" => $rs_data,
            "id" => $id,
        );
        $this->assign("data", $tvar);
        return $this->fetch('website/dynamic/boxdynamic');
    }


    /**
     * @route('website/deldynamic')
     * @return mixed
     * 删除公司动态
     */
    public function deldynamic()
    {
        $Id = $_GET['id'];
        $rs = Dynamic::destroy($Id);
        if ($rs === false) {
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }


    /**
     * @route('website/newslist')
     * @return mixed
     * 公司新闻列表
     */
    public function NewsList()
    {

        $News = new News();

        // 获取搜索条件
        $get['status'] = input('get.status');

        // 后台账号数据
        $rs_data = $News->get_list_page($get, 'status asc,sort desc', '', 10);

        $tvar = array(
            "title" => "商铺列表",
            "time" => time(),
            "data" => $rs_data,
            "get" => $get,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('website/news/newslist');
    }

    /**
     * @route('website/boxnews')
     * @return mixed
     * 编辑公司新闻
     */
    public function BoxNews(Request $request)
    {
        $news = new News();

        $id = input('id');

        if ($request->isAjax()) {
            $param = $request->param();

            if (array_key_exists("imageurl", $param)) {
                $data['top_pic'] = "/upload/" . $param['imageurl'][0];
            }

            $data['title'] = $param['title'];
            $data['content'] = $param['content'];
            $data['status'] = $param['status'];
            $data['type'] = $param['type'];
            $data['author'] = $param['author'];
            $data['source'] = $param['source'];
            $data['url'] = $param['url'];
            $data['sort'] = $param['sort'];
            $data['summry'] = $param['summry'];

            $rs = $news->edit($data, $id);
            if ($rs === false) {
                $this->error($news->getError());
            }
            $this->success('操作成功', '/website/newslist');
        }

        $rs_data = null;
        if ($id > 0) {
            $rs_data = $news->getOne(['id' => $id], '*');
        }

        $tvar = array(
            "title" => "添加公司新闻",
            "time" => time(),
            "data" => $rs_data,
            "id" => $id,
        );
        $this->assign("data", $tvar);
        return $this->fetch('website/news/boxnews');
    }


    /**
     * @route('website/delnews')
     * @return mixed
     * 删除公司新闻
     */
    public function DelNews()
    {
        $Id = $_GET['id'];
        $rs = News::destroy($Id);
        if ($rs === false) {
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }


    /**
     * @route('website/videolist')
     * @return mixed
     * 公司视频列表
     */
    public function VideoList()
    {

        $video = new Video();

        // 获取搜索条件
        $get['status'] = input('get.status');

        // 后台账号数据
        $rs_data = $video->get_list_page($get, 'status asc,sort desc', '', 10);

        $tvar = array(
            "title" => "商铺列表",
            "time" => time(),
            "data" => $rs_data,
            "get" => $get,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('website/video/videolist');
    }

    /**
     * @route('website/boxvideo')
     * @return mixed
     * 添加公司视频
     */
    public function BoxVideo(Request $request)
    {
        $video = new Video();

        $id = input('id');
        $vide_data  = null;
        if($id > 0){
            $vide_data = $video->getOne(['id' => $id]);
        }

        if ($request->isPost()) {

            $data['name'] = input('name');
            $data['sort'] = input('sort');

            if($data['sort'] == 1){
                $vi = $video->getOne(['sort' => 1],'id');
                if($vi){
                    $this->error('排序值已经有 1 了，排序值重复 1 是不会显示视频的');
                }
            }


            if(!$id){
                // 接收视频文件
                $file = \request()->file('image');

                $filename = time() . rand(0, 999);
                if ($file) {
                    $info = $file->validate(['ext' => 'mp4'])->move('../public/video/' . date('Ymd', time()) . '/', $filename);
                    if ($info) {
                        $data['video_url'] = "/video/" . date('Ymd', time()) . "/" . $info->getSaveName();
                    }
                } else {
                    if ($vide_data['video_url']) {
                        $data['video_url'] = $vide_data['video_url'];
                    } else {
                        $this->error('接收不到视频文件');
                    }
                }
            }

            $rs = $video->edit($data, $id);
            if ($rs === false) {
                $this->error($video->getError());
            }
            echo "<script>window.parent.location.reload()</script>";
        }

//        var_dump($rs);die;

        $tvar = array(
            "title" => "添加公司视频",
            "time" => time(),
            "data" => $vide_data,
            "video_id" => $id,
        );
        $this->assign("data", $tvar);
        return $this->fetch('website/video/boxvideo');
    }


    /**
     * @route('website/delvideo')
     * @return mixed
     * 删除公司视频
     */
    public function DelVideo()
    {
        $Id = $_GET['id'];
        $rs = Video::destroy($Id);
        if ($rs === false) {
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }

}
